package com.heima.leetcode.practice;

/**
 * @author 勾新杰
 * @version 1.0
 * @description: leetcode 125. 验证回文串
 * @date 2025/3/24 10:27
 */
public class E125 {

    /**
     * <h3>方法一：中心扩展法</h3>
     *
     * @param s 字符串
     * @return 是否是回文
     */
    public boolean isPalindrome1(String s) {
        String lowerCase = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
        char[] charArray = lowerCase.toCharArray();
        int length = charArray.length;
        boolean odd = (length & 1) == 1;
        int median = length >> 1;
        int left = odd ? median : median - 1, right = median;
        while (left >= 0 && right < length && charArray[left] == charArray[right]) {
            left--;
            right++;
        }
        return left < 0 && right >= length;
    }

    /**
     * <h3>方法二：两边向中心</h3>
     *
     * @param s 字符串
     * @return 是否是回文
     */
    public boolean isPalindrome2(String s) {
        String lowerCase = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
        char[] charArray = lowerCase.toCharArray();
        int left = 0, right = charArray.length - 1;
        while (left < right) {
            if (charArray[left] != charArray[right]) return false;
            left++;
            right--;
        }
        return true;
    }
}
